<!--
描述：回复管理
开发人：yyp
开发日期：2019/05/30
-->

<template lang="pug">
  div.kalix-article
    keep-alive
      el-row.duty-row(:gutter="0")
        el-col.duty-col(:span="6" style="padding:8px 0 8px 8px;")
          kalix-reply-tree(v-on:replyTreeClick="onReplyTreeClick"  style="height: 100%;")
        el-col.duty-col(:span="18")
          kalix-tree-grid-1.duty-wrapper(bizKey="messagereply" title="回复管理"
          style="padding-top:0"
          ref="kalixTreeGrid"
          v-bind:targetURL="treeUrl"
          v-bind:isToolBarSelf="true"
          v-bind:toolbarBtnList="treeToolbarBtnList"
          v-bind:onToolBarSelfClick="onToolBarClick"
          v-bind:bizDialog="bizDialog"
          v-bind:columns='columns'
          v-bind:customRender="showCheckText"
          v-on:selectedRow="getSelectRow"
          v-bind:isRowButtonSelf="true"
          v-bind:btnSelfClick="btnClick"
          v-bind:jsonStr="jsonStr"
          v-bind:noSearchParam:sync="noSearchParam"
          v-bind:isColumnfixed="false"
          v-bind:optActions="optActions"
          v-bind:btnList="btnList"
          v-bind:buttonPermissionPrefix="buttonPermissionPrefix" )
</template>

<script type="text/ecmascript-6">
  import FormModel from './model'
  import {messagereplyConfigBtnList} from './config'
  import Message from '../../../../../common/message'
  import {replyItemBaseURL, MessageReplyURL, replyBtnPermissionPrefix, MessageURL} from '../../config.toml'
  import KalixReplyTree from '../../../../../components/cascader/messagereplyTree'
  import KalixTreeGrid from '../../../../../components/forum/treeGrid'
  import EventBus from '../../../../../components/corelib/common/eventbus'
  import {ON_REFRESH_DATA} from '../../../../../components/corelib/components/common/event.toml'

  export default {
    name: 'kalix-mplive-messagereply',
    data() {
      return {
        btnList: messagereplyConfigBtnList,
        name: '',
        noSearchParam: true,
        itemBasePath: replyItemBaseURL,
        buttonPermissionPrefix: replyBtnPermissionPrefix,
        toolbarBtnList: [
          {id: 'add', isShow: false, title: '添加', isPermission: false},
          {id: 'refresh', isShow: true, icon: 'icon-refresh', title: '刷新'}
        ],
        treeToolbarBtnList: [
          {id: 'add', isShow: false, title: '添加', isPermission: false},
          {id: 'refresh', isShow: true, icon: 'icon-refresh', title: '刷新'}
        ],
        // targetUrl: replyMenuURL,
        treeUrl: MessageReplyURL + '/getMessageReplyByPostId?messageId=-1',
        menuItems: [],
        addFormModel: Object.assign({}, FormModel),
        editFormModel: Object.assign({}, FormModel),
        auditingFormModel: Object.assign({}, FormModel),
        messageId: undefined,
        froumTitle: undefined,
        parentId: undefined,
        kalixDialog: undefined,
        currentRow: undefined,
        isIconSelf: true,
        replyNum: undefined,
        jsonStr: '',
        bizDialog: [
          {id: 'add', dialog: 'messagereplyAdd'},
          {id: 'edit', dialog: 'messagereplyEdit'}
        ],
        optActions: {
          title: '操作',
          type: 'action',
          actions: [{
            type: 'edit',
            text: '编辑',
            icon: 'el-icon-edit'
          }, {
            type: 'delete',
            text: '删除',
            icon: 'el-icon-delete'
          }, {
            type: 'auditing',
            text: '审核',
            icon: 'el-icon-auditing'
          }],
          width: '150'
        },
        columns: [{
          type: 'hidden',
          key: 'id',
          width: '0'
        }, {
          type: 'hidden',
          key: 'parentId',
          width: '0'
        }, {
          title: '回复人姓名',
          key: 'userName',
          width: '150'
        }, {
          title: '回复内容',
          key: 'replyMessage',
          width: '120'
        }, {
          title: '回复时间',
          key: 'updateDate',
          width: '120'
        }, {
          type: 'hidden',
          key: 'category',
          width: '0'
        }, {
          type: 'hidden',
          key: 'messageId',
          width: '0'
        }]
      }
    },
    components: {
      KalixReplyTree,
      KalixTreeGrid1: KalixTreeGrid
    },
    computed: {},
    mounted() {
      this.jsonStr = `{ '%userName%': ''}`
    },
    methods: {
      onReplyTreeClick(data) {
        this.replyNum = data.replyNum
        this.uid = data.uid
        this.messageId = data.value
        this.parentContent = data.label
        this.parentContent = data.replyMessage
        this.jsonStr = `{'%userName%': ''}`
        this.treeUrl = MessageReplyURL + '/getMessageReplyByPostIdH?messageId=' + data.value
        this.dialogOptions = {
          messageId: data.value,
          parentContent: data.label
        }
      },
      showCheckText(_data) {
        this.showCheck(_data)
      },
      showCheck(_data) {
        if (_data) {
          _data.forEach((e) => {
            e.categoryName = e.category === '0' ? '未审核' : '已审核'
            if (e.children) {
              this.showCheck(e.children)
            }
          })
        }
      },
      onToolBarClick(btnId) {
        if (btnId === 'add') {
          this.onAddClick()
        }
        if (btnId === 'refresh') {
          this.onRefreshClick()
        }
      },
      btnClick(row, btnId) {
        if (btnId === 'edit') {
          this.onEditClick(row)
        }
        if (btnId === 'delete') {
          this.onDeleteClick(row)
        }
        if (btnId === 'auditing') {
          this.onAuditingClick(row)
        }
      },
      onRefreshClick() {
        if (this.treeUrl !== undefined) {
          this.$refs.kalixTreeGrid.getData()
        }
      },
      onAddClick() {
        if (this.parentContent === undefined || this.messageId === undefined) {
          Message.error('请选择要回复的帖子！')
          return
        }
        let that = this
        this.$refs.kalixTreeGrid.getKalixDialog('add', (_kalixDialog) => {
          this.kalixDialog = _kalixDialog
          setTimeout(() => {
            this.addFormModel.parentContent = this.parentContent
            this.addFormModel.messageId = this.messageId
            if (this.currentRow === undefined) {
              this.addFormModel.parentName = '根目录'
              this.addFormModel.parentId = '-1'
              this.addFormModel.parentContent = this.parentContent
              this.addFormModel.userName = this.userName
            } else {
              this.addFormModel.parentName = this.currentRow.username
              this.addFormModel.parentId = this.currentRow.id
              this.addFormModel.parentContent = this.currentRow.replyMessage
              this.addFormModel.userName = this.userName
            }
            this.addFormModel.isLeaf = '0'
            this.kalixDialog.$refs.kalixBizDialog.open('添加', false, this.addFormModel)
            if (typeof (that.kalixDialog.init) === 'function') {
              that.kalixDialog.init(this.dialogOptions) //  需要传参数，就在dialog里面定义init方法
            }
          }, 20)
        })
      },
      onEditClick(row) {
        let that = this
        this.$refs.kalixTreeGrid.getKalixDialog('edit', (_kalixDialog) => {
          this.kalixDialog = _kalixDialog
          setTimeout(() => {
            this.editFormModel = row
            this.editFormModel.parentContent = this.parentContent
            this.editFormModel.parentContent = this.editFormModel.replyMessage
            if (row.dataPermission !== true) {
              row.dataPermission = false
            }
            this.editFormModel.dataPermission = row.dataPermission + ''
            console.log('this.editFormModel==============', this.editFormModel)
            this.kalixDialog.$refs.kalixBizDialog.open('编辑', true, this.editFormModel)
            if (typeof (that.kalixDialog.init) === 'function') {
              that.kalixDialog.init(this.dialogOptions) // 需要传参数，就在dialog里面定义init方法
            }
          }, 20)
        })
      },
      onDeleteClick(row) {
        this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return this.axios.request({
            method: 'DELETE',
            url: MessageReplyURL + '/' + row.id,
            params: {},
            data: {
              id: row.id
            }
          })
        }).then(response => {
          this.axios.request({
            method: 'PUT',
            url: MessageURL + '/' + row.messageId,
            data: {
              id: row.messageId,
              replyNum: this.replyNum - 1,
              uid: this.uid
            }
          }).then(response => {
            this.$refs.kalixTreeGrid.getData()
            if (response.data.success === true) {
              Message.success('删除成功')
            } else {
              Message.success('删除失败')
            }
            EventBus.$emit(ON_REFRESH_DATA, 'moocClassExamine')
          })
        }).catch(() => {
        })
      },
      getSelectRow(val) {
        console.log('getSelectRow========', val)
        this.currentRow = val
      }
    }
  }
</script>

<style scoped lang="stylus" type="text/stylus">
  @import "../../../../../assets/stylus/baseTable.styl"
  @import "../../../../../assets/stylus/color.styl"
  .kalix-article
    position relative
    height 100%
    overflow hidden
    box-sizing border-box

    .duty-row
      height 100%
      .duty-col
        height 100%
        box-sizing border-box

    .duty-wrapper
      margin 8px 0
      margin-left 30px
      .kalix-wrapper
        bottom 0 !important
      label
        overflow hidden
        height 28px
        text-overflow ellipsis
        white-space nowrap
</style>
